package com.walter.web.action;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.Authenticator.Success;
import com.walter.domain.Personal;
import com.walter.service.PersonalService;
import com.walter.service.impl.PersonalServiceImpl;

public class SavePersonalInfoAction extends ActionSupport implements ServletRequestAware {

	private HttpServletRequest request;
	private PersonalService pService = new PersonalServiceImpl();

	public String execute() {
		// 1.获得参数并封装到Customer对象
		Personal p = new Personal();
		try {
			BeanUtils.populate(p, request.getParameterMap());
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println(p.getP_id());

		// 2调用Service保存客户
		if(p.getP_id()==0){
		pService.saveInfo(p);}
		
		if(p.getP_id()!=0){
		pService.updatePerson(p);
		}
		//Personal oldP = pService.getById(p.getP_id());
		//pService.updatePerson(oldP);

		request.getSession().setAttribute("personalInfo", p);
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

}
